<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
	 <meta charset="utf-8">
	 <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	 <title th:replace="common/base::pagetitle"></title>
	 <link th:replace="common/base::static"/>
</head>
<body style="background-color: #f2f2f2;">

<div id="kPage" class="layui-fluid" style="padding: 0">
	 <div class="layui-row">
		  <!-- 主要内容块 -->
		  <div class="layui-col-md12" id="kMainContentCol">
				<div class="layui-fluid wlb-fluid-padding15">
					 <div class="layui-card">
						  <form>
								<div class="layui-form layui-card-header wlb-card-header-auto">
									 <div class="layui-form-item">
										  <div class="layui-inline">
												<label class="layui-form-label">合同编号</label>
												<div class="layui-input-inline">
													 <input type="text" name="contractNo" placeholder="请输入" autocomplete="off" class="layui-input">
												</div>
										  </div>
										  <div shiro:hasAnyRoles="admin" class="layui-inline">
												<label class="layui-form-label">供应商</label>
												<div class="layui-input-inline">
													 <select name="supplierId" class="form-control" >
														  <option value="">请选择供应商</option>
														  <option th:each="supplier:${supplierList}" th:value="${supplier.id}" th:text="${supplier.name}"></option>
													 </select>
												</div>
										  </div>
										  <div shiro:hasAnyRoles="admin" class="layui-inline">
												<label class="layui-form-label">核心企业</label>
												<div class="layui-input-inline">
													 <select name="coreEnterpriseId" class="form-control" >
														  <option value="">请选择核心企业</option>
														  <option th:each="core:${coreList}" th:value="${core.id}" th:text="${core.name}"></option>
													 </select>
												</div>
										  </div>
										  <div class="layui-inline">
												<button class="layui-btn " lay-submit="" lay-filter="kTableSearchFilter">
													 <i class="layui-icon layui-icon-search"></i>
												</button>
												<button class="layui-btn " type="reset">
													 <i class="layui-icon layui-icon-refresh-1"></i>
												</button>
										  </div>
									 </div>
								</div>
						  </form>

						  <div class="layui-card-body">
								<table id="kContentTable" lay-filter="kContentTable"></table>
						  </div>
					 </div>

				</div>
		  </div>
	 </div>
</div>



<script th:replace="common/base::context"></script>
<script th:replace="common/base::mainjs"></script>

<script type="text/html" id="coreEnterpriseIdTpl">
	 {{# var data = wlbKit.get(api.sys.getDept + d.coreEnterpriseId).data; }}
	 {{# return data ? data.name : '<span class="layui-badge-rim">未分配</span>'; }}
</script>
<script type="text/html" id="supplierIdTpl">
	 {{# var data = wlbKit.get(api.sys.getDept + d.supplierId).data; }}
	 {{# return data ? data.name : '<span class="layui-badge-rim">未分配</span>'; }}
</script>

<!--头部工具栏模板-->
<script type="text/html" id="headToolbarTpl">
</script>

<!--表格行工具栏模板-->
<script type="text/html" id="rowToolbarTpl">
	 <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>详情</a>
	 <a shiro:hasPermission="voucher:generate"  class="layui-btn layui-btn-normal layui-btn-xs" lay-event="generate"><i class="layui-icon layui-icon-edit"></i>生成凭证</a>
</script>

<script type="text/javascript" th:inline="javascript">




    layui.use(['element', 'form', 'layer', 'table'], function () {
        var elemet = layui.element, form = layui.form, layer = layui.layer, table = layui.table;
        var tableId = 'kContentTable';

        // 实例化table
        var tableIns = table.render($.extend(config.layui.table, {
            elem: '#' + tableId
            , url: BASE_WEB + 'trade/list/data' //数据接口
            //, toolbar: '#headToolbarTpl'
            , cols: [[ //表头
                {type: 'checkbox', fixed: 'left'}
                , {field: 'contractNo', title: '合同编号', width: 120}
                , {field: 'contractAmount', title: '合同金额', width: 100}
                , {field: 'coreEnterpriseId', title: '核心企业',  width: 120, templet: '#coreEnterpriseIdTpl'}
                , {field: 'supplierId', title: '供应商', width: 120, templet: '#supplierIdTpl'}
                , {field: 'bankId', title: '银行id', hide: true}
                , {field: 'projectName', title: '项目名称', width: 120}
                , {field: 'tradeDate', title: '交易日期', sort: true, width: 150}
                , {field: 'tradeTime', title: '交易时间', sort: true, width: 150}
                , {field: '', title: '操作', width: 235, fixed: 'right', templet: '#rowToolbarTpl'}
            ]]
		  }));

        var area = ['80%', '60%'];


        // 表格行按钮 监听事件
        table.on('tool(' + tableId + ')', function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象
            if (layEvent === 'edit') {

                layer.open({
                    type: 2
                    , title: '数据详情'
                    , content:api.trade.tradeInfoView + '?id=' + data.id 
                    , maxmin: true
                    , area: area   // 宽高
                    , btn: ['关闭']
                });
            }
            if (layEvent === 'generate') {
                //先判断是否有关联银行
                layer.open({
                    type: 2
                    , title: '选择模板'
                    , content: BASE_WEB + 'voucher/generate/choice/templateView' + '?compId=' + data.coreEnterpriseId + '&bankId=' + data.bankId
                    , maxmin: true
                    , area: ['50%', '40%']   // 宽高
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var templateObj = window["layui-layer-iframe" + index].getCheck();
                        if (templateObj == null || templateObj.id == null) {
                            wlbKit.errorMsg("请选择模板！");
                            return false;
                        }
                        var templateId = templateObj.id;
                        var bankId = templateObj.bankId;
                        console.log(bankId+"dsfsdfdsf")
                        layer.close(index);//关闭弹出层

                        layer.open({
                            type: 2
                            , title: '选择验收数据'
                            , content: BASE_WEB + 'voucher/generate/selAcceptanceInfo' + '?tradeId=' + data.id+"&bankId=" + bankId+"&supplierId=" + data.supplierId + "&templateId="+templateId
                            , maxmin: true
                            , area: ['80%', '60%']   // 宽高
                            , btn: ['确定', '取消']
                            , yes: function (index1, layero1) {
                                //执行弹出页面getCheck方法，获取选择中的id
                                var ids = window["layui-layer-iframe" + index1].getCheck();
                                if (ids.length <= 0) {
                                    wlbKit.errorMsg("请选择至少选择一条验收数据");
                                    return false;
                                }
                                layer.close(index1);//关闭弹出层
                                //打开预览页面
                                layer.open({
                                    type: 2
                                    , title: '凭证预览'
                                    , content: BASE_WEB + 'voucher/generate/preview' + '?tradeId=' + data.id+"&bankId=" + bankId+"&supplierId=" + data.supplierId+"&ids=" + ids+"&templateId=" + templateId
                                    , maxmin: true
                                    , area: ['80%', '60%']   // 宽高
                                    , btn: ['生成', '取消']
                                    , yes: function (index2, layero2) {  //点击确认触发 iframe 内容中的按钮提交
                                        var submit = layero2.find('iframe').contents().find("#kFormSubmit");
                                        submit.click();
                                        layer.close(index2);//关闭弹出层
                                    }
                                });
                            }
                        });
                    }
                });

            }

        });

        // 监听查询
        form.on('submit(kTableSearchFilter)', function (obj) {
            var params = obj.field;
            log('seach=', params);//
            tableIns.reload({
                where: params,
                page: {curr: 1}
            });
            return false;
        });

        // 绑定监听回车事件
        wlbKit.bindEnterEventForLayuiForm();

    });
</script>

</body>
</html>
